/**
 * 
 */
package org.bankabeule.ui.wizards;

import org.bankabeule.Account;
import org.bankabeule.AccountTypeEnum;
import org.bankabeule.BankabeuleFactory;
import org.bankabeule.MoneyEnum;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author sdudoit
 *
 */
public class AccountPage extends WizardPage {

	
	/**
	 * Account
	 */
	private Account account;
	
	
	/**
	 * Constructor
	 * @param pageName
	 */
	protected AccountPage(String pageName, Account existingAccount) {
		super(pageName);
		if (existingAccount == null) {
			account = BankabeuleFactory.eINSTANCE.createAccount();
			account.setId("");
			account.setName("No name");
			account.setBankid("123456");
			account.setBranchid("7890");
			account.setType(AccountTypeEnum.CHECKING);
			account.setInitValue(0.00);
			account.setValue(0.00);
			account.setMoney(MoneyEnum.EUR);
			account.setDescription("");
		} else {
			account = (Account) EcoreUtil.copy(existingAccount);
		}
		setTitle("Set account properties");
		setPageComplete(!account.getId().equals(""));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		FormToolkit tk = new FormToolkit(parent.getDisplay());
		
		parent.setLayout(new FillLayout());
		
		Composite mainComposite = tk.createComposite(parent);
		mainComposite.setLayout(new GridLayout(2, false));
		
		if (account.getId().equals("")) {
			tk.createLabel(mainComposite, "Id").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
			Text text = tk.createText(mainComposite, account.getId());
			text.setLayoutData(GridDataFactory.fillDefaults().create());
			text.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					account.setId(((Text) e.getSource()).getText());
					setPageComplete(!account.getId().equals(""));
				}
			});
		}
		
		tk.createLabel(mainComposite, "Name").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		Text text = tk.createText(mainComposite, account.getName());
		text.setLayoutData(GridDataFactory.fillDefaults().create());
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				account.setName(((Text) e.getSource()).getText());
			}
		});
		
		tk.createLabel(mainComposite, "Bank Id").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		text = tk.createText(mainComposite, account.getBankid());
		text.setLayoutData(GridDataFactory.fillDefaults().create());
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				account.setBankid(((Text) e.getSource()).getText());
			}
		});
		
		tk.createLabel(mainComposite, "Branch Id").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		text = tk.createText(mainComposite, account.getBranchid());
		text.setLayoutData(GridDataFactory.fillDefaults().create());
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				account.setBranchid(((Text) e.getSource()).getText());
			}
		});		
		
		tk.createLabel(mainComposite, "Type").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		Combo combo = new Combo(mainComposite, SWT.NONE);
		combo.setLayoutData(GridDataFactory.fillDefaults().create());
		for (AccountTypeEnum value : AccountTypeEnum.values()) {
			combo.add(value.getLiteral(), value.getValue());
		}
		combo.select(account.getType().getValue());
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				account.setType(AccountTypeEnum.get(((Combo) e.getSource()).getText()));
			}
		});	
		
		tk.createLabel(mainComposite, "Initial value").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		text = tk.createText(mainComposite, String.format("%.2f", account.getInitValue()));
		text.setLayoutData(GridDataFactory.fillDefaults().create());
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				try {
					String text = ((Text) e.getSource()).getText();
					double value = Double.parseDouble(text.replace(',', '.'));
					account.setInitValue(value);
					setErrorMessage(null);
					setPageComplete(!account.getId().equals(""));
				} catch (Exception ex) {
					setErrorMessage(ex.getMessage());
					setPageComplete(false);
				}
			}
		});		
		
		tk.createLabel(mainComposite, "Money").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		combo = new Combo(mainComposite, SWT.NONE);
		combo.setLayoutData(GridDataFactory.fillDefaults().create());
		for (MoneyEnum value : MoneyEnum.values()) {
			combo.add(value.getLiteral(), value.getValue());
		}
		combo.select(account.getType().getValue());
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				account.setMoney(MoneyEnum.get(((Combo) e.getSource()).getText()));
			}
		});	
		
		Label descLabel = tk.createLabel(mainComposite, "Description");
		descLabel.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 2, 1));
		text = tk.createText(mainComposite, account.getDescription(), SWT.MULTI);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 2));
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				account.setDescription(((Text) e.getSource()).getText());
			}
		});	
		
		setControl(parent);
	}
	
	
	/**
	 * Get account
	 */
	public Account getAccount() {
		return account;
	}

}
